

import 'dart:math';

import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_date_tool.dart';
import 'package:bt_tool/basic/bt_db_tool.dart';
import 'package:bt_tool/basic/bt_json_tool.dart';
import 'package:bt_tool/basic/bt_notification_center.dart';
import 'package:bt_tool/basic/bt_router.dart';
import 'package:bt_tool/widget/bt_form_select_widget.dart';
import 'package:bt_tool/widget/bt_text_field.dart';
import 'package:bt_tool/widget/bt_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_demo/tools/donext/model/do_next_model.dart';
import 'package:flutter_demo/tools/donext/do_next_define.dart';
import 'package:flutter_demo/tools/project/model/project_model.dart';
import 'package:get/get.dart';

import '../../../common/config/app_router.dart';
import '../../project/project_tool.dart';


class DoNextCreateController extends BTController{

  final BTTextFieldController inputCtl = BTTextFieldController();
  var endTimeType = 0.obs;
  DoNextModel? detModel;
  var selectProjectId = 0;
  var projectSelectModelList = <BTFormSelectModel>[].obs;

  @override
  Future<void> initState() async {
    super.initState();

    projectSelectModelList.add(BTFormSelectModel(0, "无", true));
    var projectModelList = await ProjectTool.getProjectData();
    for(var model in projectModelList){
      projectSelectModelList.add(BTFormSelectModel(model.id,model.name,false));
    }
    var id = routerJsonTool.getInt("id",defaultValue: -1);
    if (id != -1){
      var result = await BTDbTool.share().query(DoNextDefine.doNextTableName,where: "id = ?",whereArgs: [id]);
      if (result.isNotEmpty){
        var map = result.first;
        detModel = DoNextModel(map);
        for(var model in projectSelectModelList){
          if(model.id == detModel!.pid){
            model.isSelect = true;
            selectProjectId = model.id;
          }else{
            model.isSelect = false;
          }
        }
        projectSelectModelList.refresh();
        int leftTime = detModel!.endTime - BTDateTool.currentTimeSeconds();
        if(leftTime > 0){
          endTimeType.value = (leftTime / 3600 / 24).toInt();
        }else{
          endTimeType.value = -1;
        }
        inputCtl.updateText(detModel!.content);
      }
    }
  }

  @override
  dispose() {
    inputCtl.dispose();
    super.dispose();
  }

  create(BuildContext context) async {
    String content = inputCtl.getText();
    if(content.isEmpty){
      BTToast.showToast("请输入内容");
      return;
    }

    if(selectProjectId == 0){
      BTToast.showToast("请选择项目");
      return;
    }

    if(detModel != null){

      detModel!.content = content;
      detModel!.pid = selectProjectId;
      late Map<String,dynamic> updateMap;
      if(endTimeType.value > -1){
        DateTime currentTime = DateTime.now();
        DateTime endDateTime = DateTime(currentTime.year,currentTime.month,currentTime.day + endTimeType.value,23,59,59);
        detModel!.endTime = (endDateTime.millisecondsSinceEpoch / 1000).toInt();
        updateMap = detModel!.convertToDictOnly(["content","pid","endTime"]);
      }else{
        updateMap = detModel!.convertToDictOnly(["content","pid"]);
      }
      var updateStatus = await BTDbTool.share().update(DoNextDefine.doNextTableName,updateMap,where: "id = ?",whereArgs: [detModel!.id]);
      if(updateStatus == BTDbTool.sqlSucAction){
        BTToast.showToast("更新成功");
        BTNotificationCenter.share().postNotifyObj(DoNextDefine.doNextCreateSucNotify, detModel!.id);
        BTRouter.share().pop();
      }else{
        BTToast.showToast("更新失败");
      }
      return;
    }

    int currentSeconds = BTDateTool.currentTimeSeconds();
    DateTime currentTime = DateTime.now();
    DoNextModel model = DoNextModel(null);
    model.content = content;
    model.createTime = currentSeconds;
    model.startTime = currentSeconds;
    model.pid = selectProjectId;

    DateTime endDateTime = DateTime(currentTime.year,currentTime.month,currentTime.day + endTimeType.value,23,59,59);
    model.endTime = (endDateTime.millisecondsSinceEpoch / 1000).toInt();
    var dataMap = model.convertToDict().btCreateMapInField([
      "content",
      "createTime",
      "startTime",
      "endTime",
      "status",
      "isDel",
      "pid",
    ]);
    await BTDbTool.share().insertData(DoNextDefine.doNextTableName, dataMap);
    BTToast.showToast("创建成功");
    if(context.mounted){
      BTRouter.share().pop(context: context);
      BTNotificationCenter.share().postNotify(DoNextDefine.doNextCreateSucNotify);
    }

  }



}